package com.example.demo.util;

import org.opencv.core.*;
import org.opencv.features2d.*;
import org.opencv.imgcodecs.Imgcodecs;

public class SIFTExample {
    public static void main(String[] args) {
//        System.setProperty("java.library.path", "D:\\Program Files\\opencv\\opencv\\build\\java");
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // 读取两张图片
        Mat img1 = Imgcodecs.imread("D:/input/01-05.jpg", Imgcodecs.IMREAD_GRAYSCALE);
        Mat img2 = Imgcodecs.imread("D:/input/009_11.png", Imgcodecs.IMREAD_GRAYSCALE);

        // 创建 SIFT 特征检测器和描述符提取器
        SIFT sift = SIFT.create();

        // 检测关键点和计算描述符
        MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
        MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
        Mat descriptors1 = new Mat();
        Mat descriptors2 = new Mat();
        sift.detectAndCompute(img1, new Mat(), keypoints1, descriptors1);
        sift.detectAndCompute(img2, new Mat(), keypoints2, descriptors2);

        // 创建特征匹配器
        BFMatcher matcher = BFMatcher.create();
        MatOfDMatch matches = new MatOfDMatch();
        matcher.match(descriptors1, descriptors2, matches);

        // 计算匹配的特征点数量
        int matchCount = matches.toList().size();
        System.out.println("匹配的特征点数量: " + matchCount);
    }
}
